﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.WebApp.WebControls;
using System.Text;

namespace ZX.WebApp.WorkFlow
{
    public partial class WorkFlowRules : System.Web.UI.Page
    {
        //调用流程服务接口实例
        CrmWorkFlowService.WorkFlowServiceClient workFlowServiceClient = null;
        public List<AppSystem> listappSystem = new List<AppSystem>(); //系统集合
        public List<AppModel> listappModel = new List<AppModel>(); // 模块集合
        public AppFunc AppFunc = new AppFunc(); //功能集合
        public AppSystem appSystem = new AppSystem(); //系统
        public AppModel appModel = new AppModel(); //模块
        public  FlowXmlDefine flowXml = new FlowXmlDefine(); //加载获取xml值类
        public string systemValue = string.Empty; //系统ObjectFolder值
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                workFlowServiceClient = new CrmWorkFlowService.WorkFlowServiceClient();
                GetSystem(); //加载系统
                GetModel(listappSystem[0].ObjectFolder);//加载模块
            }
        }

        /// <summary>
        /// 系统
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlSystem_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblError.Text = "";
            systemValue = ddlSystem.SelectedItem.Value;
            int i = ddlSystem.SelectedIndex;
            GetSystem(); //加载系统
            ddlSystem.SelectedIndex = i;
            appSystem = listappSystem.Where(p => p.Name == systemValue).FirstOrDefault();
            if (appSystem != null)
               GetModel(appSystem.ObjectFolder);////加载模块
        }
        /// <summary>
        /// 模块
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlModel_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblError.Text = "";
            string strMsgOpen = string.Empty;

            systemValue = ddlSystem.SelectedItem.Value;
            int i = ddlSystem.SelectedIndex;
            GetSystem(); //加载系统
            ddlSystem.SelectedIndex = i;
            appSystem = listappSystem.Where(p => p.Name == systemValue).FirstOrDefault();
            
      
            //--------------------------------------------------------------------------------
            string modelValue = ddlModel.SelectedItem.Value;
            int y = ddlModel.SelectedIndex;
            if (modelValue.ToString() != "")
            {
                AppFunc = flowXml.ListSystemFunc(appSystem.ObjectFolder, modelValue, strMsgOpen);
            }
            if (AppFunc != null)
            {
                txbMethodName.Text = AppFunc.PublicMethod; //方法名
                txbClassName.Text = AppFunc.ParamterCalss; //参数实体
                txbIserver.Text = AppFunc.PublicCalss; //服务接口
                txbAddress.Text = AppFunc.ServiceWsdlUri; //服务地址

                //StringBuilder parames = new StringBuilder();
                //foreach(var item in AppFunc.Parameter)
                //{
                //    if(parames.Length == 0)
                //    {
                //        parames.Append(item.Name);
                //    }
                //    else
                //    {
                //        parames.Append(","+item.Name);
                //    }
                    
                //}
                ddlParameter.DataSource = AppFunc.Parameter;
                ddlParameter.DataValueField = "Name";
                ddlParameter.DataTextField = "Description";
                ddlParameter.DataBind();
                lblvisid.Text = AppFunc.WorkFlowId; //流程ID
                lblObjectFolder.Text = AppFunc.ObjectFolder;
            }
            
        }
        /// <summary>
        /// 流程状态
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlStaus_SelectedIndexChanged(object sender, EventArgs e)
        {
            lblError.Text = "";
        }
        /// <summary>
        /// 加载系统
        /// </summary>
        public void GetSystem()
        {
            listappSystem = flowXml.ListSystem();//系统名称
            ddlSystem.DataValueField = "Name";
            ddlSystem.DataTextField = "Description";
            this.ddlSystem.DataSource = listappSystem;
            ddlSystem.DataBind();
        }

        /// <summary>
        /// 加载模块
        /// </summary>
        /// <param name="ObjectFolder"></param>
        public void GetModel(string ObjectFolder)
        {
            listappModel = flowXml.AppModel(ObjectFolder);//模块名称
            if (listappModel.Count > 0)
            {
                ddlModel.DataValueField = "Name";
                ddlModel.DataTextField = "Description";
                this.ddlModel.DataSource = listappModel;
                ddlModel.DataBind();
            }
            else 
            {
                this.ddlModel.DataSource = null;
                ddlModel.DataBind();
            }
        }
        /// <summary>
        /// 确定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnOk_Click(object sender, EventArgs e)
        {
            workFlowServiceClient = new CrmWorkFlowService.WorkFlowServiceClient();
            string appsystem = ddlSystem.SelectedItem.Value; //系统
            string appmodel = ddlModel.SelectedItem.Value; //模块
            string methodName = string.Empty; 
            if (this.txbMethodName.Text != "") //方法名称
            {
                methodName = this.txbMethodName.Text;
            }
            else 
            {
                return;
            }
            string className = txbClassName.Text; //参数实体
            string iserver = txbIserver.Text; //服务接口
            string address = txbAddress.Text; //服务地址
            string objectFolder = lblObjectFolder.Text;
            List<CrmWorkFlowService.ParameterEntity> parameters = new List<CrmWorkFlowService.ParameterEntity>();
            string strMsgOpen = string.Empty;
            AppFunc = flowXml.ListSystemFunc(objectFolder, appmodel, strMsgOpen);
            foreach (ParameterEntity item in AppFunc.Parameter)
            {
                CrmWorkFlowService.ParameterEntity parter = new CrmWorkFlowService.ParameterEntity();
                parter.AppModel = appmodel;
                parter.Name = item.Name;
                parter.Description = item.Description;
                parter.Value = item.Value;
                parter.Sataus = item.Sataus;
                parameters.Add(parter);
            }
            char satues = Convert.ToChar(ddlStaus.SelectedItem.Value); //流程状态
            string workFlowId = lblvisid.Text; //流程ID

            int i = workFlowServiceClient.AddLoadIserver(appsystem, appmodel, methodName, className, iserver, address, "", satues, workFlowId, objectFolder, parameters);
            if (i > 0)
            {
                lblError.Text = "保存成功";
            }
            else 
            {
                lblError.Text = "保存失败";
            }
        }
     
   }
}